package hanshunping.project.tankWar;

import java.util.ArrayList;
import java.util.HashSet;

public class EnemyTank implements Runnable{
    int x;
    int y;
    boolean live = true;
    boolean exist = true;
    public EnemyTank(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    void shot(){ }
    void move(){ }

    HashSet<EnemyBullet> getBull(){
        return new HashSet();
    }

    @Override
    public void run() {

    }
}

class EA extends EnemyTank{
    int bx;
    int by;
    Object lock;
    HashSet<EnemyBullet> ebs = new HashSet<>();
    public EA(int x, int y,Object lock) {
        super(x, y);
        bx = x;
        by = y;
        this.lock = lock;
        new Thread(this).start();
        move();
    }
    void shot(){
        EnemyBullet eb = new EnemyBullet(x,y+13,10);
        synchronized (lock){
        ebs.add(eb);}
        new Thread(){
            public void run(){
                while(eb.x>=0) {
                    eb.x -= 7;
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                eb.live=false;
            }
        }.start();

    }
    void move(){
        final double[] move = {0};
        int move2 =10;
        new Thread(){
            public void run(){
                while(x>=-50){
                    x-=3;
                    x=x+(int)(move[0]*10)-5;
                    y=y+(int)(move[0]*10)-5;
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                exist = false;
            }
        }.start();
        new Thread(){
            public void run(){
                while(x>=-50){
                    move[0] =Math.random();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                exist = false;
            }
        }.start();
    }
    HashSet<EnemyBullet> getBull(){
        return ebs;
    }


    public void run(){
        while(x>0&&this.live){
            shot();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class Enemys extends Thread{
    Object lock = new Object();
    ArrayList<EnemyTank> ets = new ArrayList<>();
    public Enemys(Object lock){
        this.lock = lock;
    }
    public void run(){
        while (true) {
            synchronized (lock){
            ets.add(new EA(1000, (int) (Math.random() * 1000),lock));
            ets.add(new EA(1000, (int) (Math.random() * 1000),lock));}
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}